package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.common.yun.enums.DataUpStateEnum;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.sfrobot.entity.RobotError;
import com.lizhi.yun.db.sfrobot.entity.TrainingRecord;
import com.lizhi.yun.db.sfrobot.repository.TrainingRecordRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/11/13 9:28
 */
@Slf4j
@Service
public class RecvTrainingRecordHandler {

    @Resource
    private TrainingRecordRepo trainingRecordRepo;

    public String recvTrainingRecord(String json){
        UpDataPOJO upPatientPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upPatientPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        TrainingRecord trainingRecord = SamJsonUtil.toObject(upPatientPOJO.getData(), TrainingRecord.class);
        if (StringUtils.isEmpty(trainingRecord)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        TrainingRecord example = new TrainingRecord();
        example.setId(trainingRecord.id);
        example.setHospitalId(rpcClientPOJO.getOrganId());
        example.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        TrainingRecord recordFromDb = trainingRecordRepo.findOne(Example.of(example)).orElse(null);
        if (StringUtils.isEmpty(recordFromDb)){
            trainingRecord.setLid(StringUtils.newId());
        } else {
            trainingRecord.setLid(recordFromDb.lid);
        }
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        trainingRecord.setCreateTime(DateUtils.getNowDate());
        trainingRecord.setHospitalId(rpcClientPOJO.getOrganId());
        trainingRecord.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        log.info("上传的数据信息是trainingRecord:{}", trainingRecord);
        trainingRecordRepo.save(trainingRecord);
        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
